
include(GenerateExportHeader)

get_filename_component(_target_name ${CMAKE_CURRENT_SOURCE_DIR} NAME_WE)

file(GLOB _target_include_files ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
file(GLOB _target_src_files ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

add_library(${_target_name} SHARED ${_target_include_files} ${_target_src_files})

generate_export_header(${_target_name} EXPORT_FILE_NAME ${_target_name}Export.h)

set(osgEarth_Libraries)
if(${osgEarth_FOUND})
    target_compile_definitions(${_target_name} PRIVATE osgEarth_FOUND)
    set(osgEarth_Libraries osgEarth::osgEarth)
endif()

target_link_libraries(${_target_name} 
    PUBLIC 
        Qt6::Quick 
        "$<BUILD_INTERFACE:${OPENSCENEGRAPH_LIBRARIES};${osgEarth_Libraries}>")
    

set(_target_public_build_dirs
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_BINARY_DIR}
        ${OPENSCENEGRAPH_INCLUDE_DIRS}
        )
target_include_directories(${_target_name}
    PUBLIC "$<BUILD_INTERFACE:${_target_public_build_dirs}>")

set_target_properties(${_target_name} PROPERTIES
    AUTOMOC ON
    PUBLIC_HEADER "${_target_include_files};${CMAKE_CURRENT_BINARY_DIR}/${_target_name}Export.h"
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION}
    )

install(TARGETS ${_target_name} EXPORT ${_target_name}_Exports
    RUNTIME
    LIBRARY
    PUBLIC_HEADER
    INCLUDES DESTINATION include)

include(CMakePackageConfigHelpers)
configure_package_config_file(${PROJECT_SOURCE_DIR}/TargetConfig.cmake.in
    "${CMAKE_CURRENT_BINARY_DIR}/${_target_name}/${_target_name}Config.cmake"
    INSTALL_DESTINATION "lib/cmake/${_target_name}")

write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/${_target_name}/${_target_name}ConfigVersion.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY AnyNewerVersion
)

install(EXPORT ${_target_name}_Exports
    FILE
    ${_target_name}Targets.cmake
    DESTINATION
    "lib/cmake/${_target_name}"
    )

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}/${_target_name}Config.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}/${_target_name}ConfigVersion.cmake
    DESTINATION "lib/cmake/${_target_name}" )